package textbasedapp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class FileIO {

	public static final String error = "Sorry, app can not create file in \"%s\".";

	public static void methodOfFileObject() {
		File file = new File("C:\\Windows");
		if (file.exists()) {
			for (String f : file.list())
				System.out.println(f);
		}
	}

	public static File createFile(String filePath) {
		File file = new File(filePath);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				System.out.println(String.format(error, filePath));
				e.printStackTrace();
			}
		}

		return file;
	}

	public static void main(String[] args) {

		// FileIO.methodOfFileObject();
		String s;
		InputStreamReader reader = new InputStreamReader(System.in);
		BufferedReader buff = new BufferedReader(reader);
		File file = FileIO.createFile("D:\\MyFile.txt");

		PrintWriter pw;
		try {
			pw = new PrintWriter(new FileWriter(file));
		} catch (IOException e1) {
			System.out.println("The writer has something wrong.");
			e1.printStackTrace();
			return;
		}

		boolean running = true;
		while (running) {
			try {
				System.out.println("Please enter your novel line by line:");
				s = buff.readLine();
				if (s.toLowerCase().equals("end")) {
					buff.close();
					pw.close();
					running = false;
				} else
					pw.println(s);

			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
